package com.cddx.logistics.controller;

import com.cddx.logistics.domain.dto.MaterialQueryDTO;
import com.cddx.logistics.domain.pojo.Material;
import com.cddx.logistics.domain.vo.MaterialVO;
import com.cddx.logistics.service.MaterialService;
import com.cddx.logistics.util.model.R;
import com.github.pagehelper.PageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/material")
public class MaterialController {
    @Resource
    private MaterialService materialService;

    @GetMapping("/queryPage")
    public R queryPage(MaterialQueryDTO queryDTO) {
        PageInfo<MaterialVO> pageInfo = materialService.queryPage(queryDTO);
        return R.ok(pageInfo);
    }

    @PostMapping("/add")
    public R add(@RequestBody Material material) {
        try {
            // 验证必需字段
            if (StringUtils.isEmpty(material.getMaterialCode())) {
                return R.error("物料编号不能为空");
            }
            if (StringUtils.isEmpty(material.getMaterialName())) {
                return R.error("物料名称不能为空");
            }
            if (material.getMaterialType() == null) {
                return R.error("物料类型不能为空");
            }
            if (material.getSupplierId() == null) {
                return R.error("供应商ID不能为空");
            }

            return materialService.add(material) ? R.ok("添加成功") : R.error("添加失败");
        } catch (Exception e) {
            return R.error("添加失败: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody Material material) {
        try {
            if (material.getMaterialId() == null) {
                return R.error("物料ID不能为空");
            }
            if (StringUtils.isEmpty(material.getMaterialCode())) {
                return R.error("物料编号不能为空");
            }
            if (StringUtils.isEmpty(material.getMaterialName())) {
                return R.error("物料名称不能为空");
            }

            if (material.getMaterialType() == null) {
                return R.error("物料类型不能为空");
            }
            if (material.getSupplierId() == null) {
                return R.error("供应商ID不能为空");
            }


            return materialService.update(material) ? R.ok("修改成功") : R.error("修改失败");
        } catch (Exception e) {
            return R.error("修改失败: " + e.getMessage());
        }
    }

    @GetMapping("/delete")
    public R delete(Long id) {
        return materialService.delete(id) ? R.ok("删除成功") : R.error("删除失败");
    }



    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestParam("ids") String ids) {
        return materialService.deleteBatch(ids) ? R.ok("批量删除成功") : R.error("批量删除失败");
    }

    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            return materialService.importExcel(file);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败: " + e.getMessage());
        }
    }
    @GetMapping("/getMaterialByInventory")
    public R getMaterialByInventory(Long storageLocationId) {
        return materialService.getMaterialByInventory(storageLocationId);
    }
}